package com.allwees.bs.module.wms.service.impl;

import com.allwees.bs.core.modelbase.entity.BaseEntity;
import com.allwees.bs.module.base.service.impl.BoServiceImpl;
import com.allwees.bs.module.scm.bo.EnteredWarehouseBo;
import com.allwees.bs.module.scm.entity.ScmShippingOrderSku;
import com.allwees.bs.module.wms.dao.WmsAllocateOrderSkuDao;
import com.allwees.bs.module.wms.entity.WmsAllocateOrderSku;
import com.allwees.bs.module.wms.req.JoinToPickOrderReq;
import com.allwees.bs.module.wms.service.WmsAllocateOrderSkuService;
import com.allwees.bs.module.wms.service.WmsPickOrderService;
import com.allwees.core.common.constant.EStatus;
import com.allwees.core.common.util.reflect.PojoUtil;
import me.zhengjie.modules.system.service.UserService;
import me.zhengjie.modules.system.service.dto.UserDto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * wms 待分配订单订单
 *
 * @author mouhn
 * @since 2020-01-08 13:56
 */
@Service
public class WmsAllocateOrderSkuServiceImpl extends BoServiceImpl<WmsAllocateOrderSkuDao, WmsAllocateOrderSku> implements WmsAllocateOrderSkuService {

    @Autowired
    private WmsPickOrderService pickOrderService;
    @Autowired
    private UserService userService;

    @Override
    public void create(EnteredWarehouseBo enteredWarehouseBo) {
        UserDto userDto = userService.findByUuid(enteredWarehouseBo.getOperatorUuid());

        List<ScmShippingOrderSku> enteredWarehouseSkus = enteredWarehouseBo.getEnteredWarehouseSkus();
        enteredWarehouseSkus.forEach(o -> {
            WmsAllocateOrderSku allocateOrderSku = PojoUtil.copyWithOmit(new WmsAllocateOrderSku(), o, BaseEntity.fields);
            allocateOrderSku.setEStatus(EStatus.WMS_WAIT_ALLOCATE);
            allocateOrderSku.setEnteredWarehouseUserUuid(enteredWarehouseBo.getOperatorUuid())
                    .setEnteredWarehouseUserName(userDto.getUsername())
                    .setWarehouseUuid(enteredWarehouseBo.getWarehouseUuid())
                    .setShelfUuid(o.getShelfUuid())
                    .setSlotUuid(o.getSlotUuid());
            create(allocateOrderSku);
        });
    }

    @Override
    public int joinToPick(JoinToPickOrderReq req) {
        int successSum = 0;

        List<WmsAllocateOrderSku> allocateOrderSkus = findByUuids(req.getAllocateOrderSkuUuids());
        for (WmsAllocateOrderSku allocateOrderSku : allocateOrderSkus) {
            allocateOrderSku.assertStatus(EStatus.WMS_WAIT_ALLOCATE);
            if (_updateStatus(allocateOrderSku.getUuid(), EStatus.WMS_ALLOCATED)) {
                successSum++;
            }
        }
        pickOrderService.create(req.getBranchUserUuid(), allocateOrderSkus);
        return successSum;
    }

    private boolean _updateStatus(String uuid, EStatus status) {
        WmsAllocateOrderSku allocateOrderSku = new WmsAllocateOrderSku(uuid);
        allocateOrderSku.setEStatus(status);
        return updateByUuid(allocateOrderSku);
    }
}
